package com.example.mardan_demo1.service.impl;

import com.example.mardan_demo1.mapper.QxMapper;
import com.example.mardan_demo1.mapper.YaopinMapper;
import com.example.mardan_demo1.mapper.YpInfoMapper;
import com.example.mardan_demo1.pojo.PageBean;
import com.example.mardan_demo1.pojo.Result;
import com.example.mardan_demo1.pojo.Yaopin;
import com.example.mardan_demo1.pojo.hzp.HzpJinDu;
import com.example.mardan_demo1.pojo.hzp.ProcessLogs;
import com.example.mardan_demo1.pojo.qx.QxInfo;
import com.example.mardan_demo1.pojo.yp.Count;
import com.example.mardan_demo1.pojo.yp.YpInfo;
import com.example.mardan_demo1.service.YpInfoService;
import com.example.mardan_demo1.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class YpInfoServiceImpl implements YpInfoService {

    @Autowired
    private YpInfoMapper ypInfoMapper;
    @Autowired
    private YaopinMapper yaopinMapper;
    @Autowired
    private QxMapper qxMapper;

    public String getUserNameByToken() {
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        String userName = (String) userInfo.get("username");
        return userName;
    }

    @Transactional
    @Override
    public int insert(YpInfo record) {
        // 获取用户信息
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userID = (Integer) userInfo.get("id");
        record.setCreator(userID.toString());
        record.setWeiTuoDanWeiDiZhi(record.getWei_tu_dan_wei_dizhi());
        record.setWeiTuoDanWeiMingCheng(record.getWei_tu_dan_wei_ming());
        record.setWeiTuoDanWeiChuanZhen(record.getWei_tu_dan_wei_chuan_zhen());
        record.setWeiTuoDanWeiLianXiDianHua(record.getWei_tu_dan_wei_lian_xidian_hua());
        record.setWeiTuoDanWeiLianXiRen(record.getWei_tu_dan_wei_lian_xiren());
        record.setWeiTuoDanWeiDianZiYouXiang(record.getWei_tu_dan_wei_dian_zi_you_xiang());
        record.setYangPinBeiZhu(record.getYangPinXinXiBeiZhu());
        ProcessLogs log = new ProcessLogs();

        if(record.getShiFouZanCun().equals(("暂存"))) {
            record.setZhuangTai("暂存");
        } else if (record.getShiFouZanCun().equals("提交")){
            record.setZhuangTai("提交");
        } else {
            return -1;
        }
        log.setYongHuId(userID);
        Integer id = ypInfoMapper.insert(record);
        log.setYangPinId(record.getId());
        log.setCaoZuoLeiXing(record.getZhuangTai());
        log.setChuLiYiJian(record.getZhuangTai());
        log.setYangPinLeiXing("Y");
        yaopinMapper.addYaoPinProcessLog(log);
        return id;

    }

    public String getUserRoleByToken() {
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        String userRole = (String) userInfo.get("role");
        return userRole;
    }
    public Integer getUserIdByToken() {
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");
        return userId;
    }

    public Result list(String yangPinMingCheng, Integer pageSize, Integer pageNum){
        // 判断权限
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userID = (Integer) userInfo.get("id");
        // 创建pageBean对象
        PageBean<YpInfo> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<YpInfo> as = ypInfoMapper.getOwnListByUserId(userID, yangPinMingCheng);
        // 数据列表和总数、页数合并
        Page<YpInfo> p = (Page<YpInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }
    public Result getDetailById(Integer id){
        return Result.success(ypInfoMapper.getDetailById(id));
    }
    public Result khUpdateById(YpInfo ypInfo){
        Map<String,Object> userInfo = ThreadLocalUtil.get();
        Integer userID = (Integer) userInfo.get("id");
        YpInfo tempInfo = ypInfoMapper.getYpInfoById(ypInfo.getId());
        if(!tempInfo.getCreator().equals(userID.toString())){
            return Result.error("你没有权限修改");
        }
        //  如果是暂存状态下才可以修改
        if(!tempInfo.getZhuangTai().equals("暂存")){
            return Result.error("只有暂存状态下才可以修改");
        }
        ypInfoMapper.updateById(ypInfo);
        return Result.success("修改成功");
    }

    @Override
    public Result adminGetYpList(String yangPinMingCheng, Integer pageSize, Integer pageNum) {
        // 创建pageBean对象
        PageBean<YpInfo> pb = new PageBean<>();
        // 开启分页查询，自动拼接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<YpInfo> as = ypInfoMapper.adminGetYpList(yangPinMingCheng);
        // 数据列表和总数、页数合并
        Page<YpInfo> p = (Page<YpInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    public Result adminGetYaoPinDetailById(Integer id) {
        String userRole = getUserRoleByToken();
        if(!userRole.equals("admin")) {
            return Result.error("权限不足");
        }
        YpInfo ypinfo = ypInfoMapper.getYpInfoById(id);
        if(ypinfo == null){
            return Result.error("数据不存在");
        }
        return Result.success(ypinfo);
    }

    @Override
    public Result deleteByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Result kehuDelYaoPinById(Integer id) {
        Integer userId = getUserIdByToken();
        YpInfo ypinfo = ypInfoMapper.getYpInfoById(id);
        if(ypinfo == null) {
            return Result.error("数据不存在");
        }
        if(!ypinfo.getCreator().equals(userId.toString())) {
            return Result.error("你没有权限删除");
        }
        if(!ypinfo.getZhuangTai().equals("暂存")) {
            return Result.error("只有暂存状态下才可以删除");
        }
        ypInfoMapper.deleteYaoPinById(id);
        return Result.success("删除成功");
    }

    @Override
    @Transactional
    public Result kehuTiJiaoYaoPin(Integer yaoPinId) {
        Integer userId = getUserIdByToken();
        YpInfo ypinfo = ypInfoMapper.getYpInfoById(yaoPinId);
        if(ypinfo == null) {
            return Result.error("数据不存在");
        }
        if(!ypinfo.getCreator().equals(userId.toString())) {
            return Result.error("你没有权限提交");
        }
        if(!ypinfo.getZhuangTai().equals("暂存")) {
            return Result.error("只有暂存状态下才可以提交");
        }
        ypinfo.setZhuangTai("提交");
        ypInfoMapper.updateById(ypinfo);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yaoPinId);
        log.setYongHuId(userId);
        log.setCaoZuoLeiXing("提交");
        log.setChuLiYiJian("提交");
        log.setYangPinLeiXing("Y");
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("提交成功");
    }

    @Override
    public Result adminBoHuiShouLi(Integer yaoPinId, String chuLiYiJian) {
        // 管理员接口，核实权限
        String userRole = getUserRoleByToken();
        if(!userRole.equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看状态是否为提交
        YpInfo ypinf = ypInfoMapper.getYpInfoById(yaoPinId);
        if(ypinf == null) {
            return Result.error("数据不存在");
        }
        if(!ypinf.getZhuangTai().equals("提交")) {
            return Result.error("只有提交状态下才可以驳回");
        }
        ypinf.setZhuangTai("暂存");
        ypInfoMapper.updateById(ypinf);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yaoPinId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("驳回");
        log.setChuLiYiJian(chuLiYiJian);
        log.setYangPinLeiXing("Y");
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("驳回成功");
    }

    @Override
    public Result adminJieshouShouLi(Integer yaoPinId, String jianPinBianHao, String chuLiYiJian) {
        // 管理员接口，核实权限
        String userRole = getUserRoleByToken();
        if(!userRole.equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看状态是否为提交
        YpInfo ypinf = ypInfoMapper.getYpInfoById(yaoPinId);
        if(ypinf == null) {
            return Result.error("数据不存在");
        }
        if(!ypinf.getZhuangTai().equals("提交")) {
            return Result.error("只有提交状态下才可以受理");
        }
        ypinf.setZhuangTai("受理");
        ypinf.setJianPinBianHao(jianPinBianHao);
        ypInfoMapper.updateById(ypinf);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yaoPinId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("受理");
        log.setChuLiYiJian(chuLiYiJian);
        log.setYangPinLeiXing("Y");
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("受理成功");
    }

    @Transactional
    @Override
    public Result adminJianyanWanchengShangChuanFujian(Integer yangPinId, String baoGaoShuFuJian) {
        // 管理员接口，核实权限
        String userRole = getUserRoleByToken();
        if(!userRole.equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看状态是否为受理
        YpInfo ypinf = ypInfoMapper.getYpInfoById(yangPinId);
        if(ypinf == null) {
            return Result.error("数据不存在");
        }
        if(!ypinf.getZhuangTai().equals("受理")) {
            return Result.error("只有受理状态下才可以受理");
        }
        ypinf.setZhuangTai("完成");
        ypinf.setBaoGaoShuFuJian(baoGaoShuFuJian);
        ypInfoMapper.updateById(ypinf);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(yangPinId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("上传报告书");
        log.setChuLiYiJian(baoGaoShuFuJian);
        log.setYangPinLeiXing("Y");
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("上传检验报告成功");
    }

    @Override
    public Result getProcessLogsById(Integer yangPinId) {
        // 创建pageBean对象
        PageBean<HzpJinDu> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<HzpJinDu> as = yaopinMapper.getYaoPinProcessLogsById(yangPinId);
        // 数据列表和总数、页数合并
        Page<HzpJinDu> p = (Page<HzpJinDu>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    @Transactional // 两条sql语句需要同步
    public Result keHuCheXiaoTiJiaoCaoZuo(Integer yangPinId) {
        //判断该样品是否属存在、是否属于这个用户、是否处于提交状态
        Integer userId = getUserIdByToken();
        YpInfo yp = ypInfoMapper.getYpInfoById(yangPinId);
        if(yp == null) {
            return Result.error("该样品不存在");
        }
        if (!yp.getCreator().equals(userId.toString())){
            return Result.error("该样品不属于你");
        }
        if(!yp.getZhuangTai().equals("提交")) {
            return Result.error("样品只能处于提交状态下才能由用户撤回");
        }
        // 修改状态，并且创建操作日志
        yp.setZhuangTai("暂存");
        ypInfoMapper.updateById(yp);
        ProcessLogs log = new ProcessLogs();
        log.setYongHuId(userId);
        log.setCaoZuoLeiXing("提交状态撤回至暂存");
        log.setYangPinLeiXing("Y");
        log.setChuLiYiJian("提交状态撤回至暂存");
        log.setYangPinId(yangPinId);
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("撤回成功");
    }

    @Override
    public Result adminCount() {
        // 查询所有状态为提交的样品
        Count count = new Count();
        count.setYaoPinDaiShouLi( ypInfoMapper.adminGetDaiShouLiShuLiang().toString());
        count.setYaoPinWanCheng(ypInfoMapper.adminGetWanChengShuLiang().toString());
        count.setYaoPinJianYanZhong(ypInfoMapper.adminGetJianYanZhong().toString());
        return Result.success(ypInfoMapper.getCount());
    }

    @Override
    public Result qxAdd(QxInfo qx) {
        // 只有客户才能调用
        String userRole = getUserRoleByToken();
        Integer userId = getUserIdByToken();
        if(!userRole.equals("customer")) {
            return Result.error("权限不足");
        }
        // 状态只能为暂存或者提交
        if(!qx.getZhuangTai().equals("暂存") && !qx.getZhuangTai().equals("提交")) {
            return Result.error("只有暂存和保存状态的申请单才可以申请");
        }
        qx.setCreator(userId.toString());
        // 创建申请单
        // todo 暂存或者提交时添加到数据库，然后返回添加的样品的id用于生成日志
        qxMapper.add(qx);
        // 创建操作日志
        ProcessLogs log = new ProcessLogs();
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing(qx.getZhuangTai());
        log.setYangPinLeiXing("Q");
        log.setChuLiYiJian(qx.getZhuangTai());
        log.setYangPinId(qx.getId());
        yaopinMapper.addYaoPinProcessLog(log);
        return Result.success("申请成功");
    }

    @Override
    public Result updateByPrimaryKeySelective(QxInfo qx) {
        // 只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有自己创建的数据才能自己修改，别人的数据不能修改
        QxInfo sqlQx = qxMapper.findQxInfoById((int) qx.getId());
        if(sqlQx == null) {
            return Result.error("申请单不存在");
        }
        // 只能编辑暂存状态下的样品
        if(!sqlQx.getZhuangTai().equals("暂存")) {
            return Result.error("只有暂存状态的申请单才可以修改");
        }
        String userIdFromToken = getUserIdByToken().toString();
        String userIdFromSql = sqlQx.getCreator().toString();
        if (!userIdFromToken.equals(userIdFromSql)) {
            return Result.error("只有申请单创建者可以修改申请单");
        }
        // 校验通过就执行
        qxMapper.updateQxInfoById(qx);
        return Result.success("修改成功");
    }

    @Override
    public Result qxGetOwnList(String yangPinMingCheng, Integer pageSize, Integer pageNum) {
        // 只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有自己创建的数据才能查询
        Integer userId = getUserIdByToken();
        PageBean<QxInfo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<QxInfo> as = qxMapper.qxGetOwnList(userId, yangPinMingCheng);
        Page<QxInfo> p = (Page<QxInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    public Result qxUserGetOwnQxInfoByQxId(Integer qxId) {
        // 只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有自己创建的数据才能查询
        Integer userId = getUserIdByToken();
        QxInfo qxInfo = qxMapper.qxUserGetOwnQxInfoByQxId(qxId, userId);
        if(qxInfo == null) {
            return Result.error("申请单不存在");
        }
        return Result.success(qxInfo);
    }

    @Override
    @Transactional
    public Result qxKehuTiJiaoYaoPin(Integer qxId) {
        // 只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有申请单状态为暂存的才能提交
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null) {
            return Result.error("申请单不存在");
        }
        if(!qx.getZhuangTai().equals("暂存")) {
            return Result.error("只有暂存状态的申请单才可以提交");
        }
        // 状态修改为提交
        qx.setZhuangTai("提交");
        qxMapper.updateQxInfoById(qx);
        // 创建操作日志
        ProcessLogs log = new ProcessLogs();
        log.setYongHuId(getUserIdByToken());
        log.setYangPinLeiXing("Q");
        log.setChuLiYiJian("提交");
        log.setYangPinId(qxId);
        log.setCaoZuoLeiXing("提交申请单");
        qxMapper.addProcessLogs(log);
        return Result.success("提交成功");
    }

    @Override
    @Transactional
    public Result qxKeHuCheHui(Integer qxId) {
        //只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有申请单状态为提交的才能撤回
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null) {
            return Result.error("申请单不存在");
        }
        if(!qx.getZhuangTai().equals("提交")) {
            return Result.error("只有提交状态的申请单才可以撤回");
        }
        // 状态修改为暂存
        qx.setZhuangTai("暂存");
        qxMapper.updateQxInfoById(qx);
        // 创建操作日志
        ProcessLogs log = new ProcessLogs();
        log.setYongHuId(getUserIdByToken());
        log.setYangPinLeiXing("Q");
        log.setChuLiYiJian("撤回");
        log.setYangPinId(qxId);
        log.setCaoZuoLeiXing("撤回申请单");
        qxMapper.addProcessLogs(log);
        return Result.success("撤回成功");
    }

    @Override
    @Transactional
    public Result qxKeHuShanChu(Integer qxId) {
        //只有客户才能调用
        if (!getUserRoleByToken().equals("customer")) {
            return Result.error("权限不足");
        }
        // 只有申请单状态为提交的才能撤回
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null) {
            return Result.error("申请单不存在");
        }
        String creatorId = qx.getCreator();
        String userId = getUserIdByToken().toString();
        if(!creatorId.equals(userId)) {
            return Result.error("只有申请单创建者可以删除申请单");
        }
        if(!qx.getZhuangTai().equals("暂存")) {
            return Result.error("只有暂存状态的申请单才可以撤回");
        }
        // 删除申请单
        qxMapper.qxDeleteById(qxId);
        // 同时删除相关日志
        qxMapper.deleteProcessLogsByYangPinId(qxId);
        return  Result.success("删除成功");
    }

    @Override
    public Result adminQxGetDetailById(Integer id) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 查询
        QxInfo qxInfo = qxMapper.findQxInfoById(id);
        if(qxInfo == null) {
            return Result.error("申请单不存在");
        }
        return Result.success(qxInfo);
    }

    @Override
    public Result adminQxGetList(String yangPinMingCheng, Integer pageNum, Integer pageSize) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 创建pageBean对象
        PageBean<QxInfo> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(pageNum, pageSize);
        // 获取查询者的id，只查询id创建的数据
        List<QxInfo> as = qxMapper.adminQxGetListByYangPinMingCheng(yangPinMingCheng);
        // 数据列表和总数、页数合并
        Page<QxInfo> p = (Page<QxInfo>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);

    }

    @Override
    @Transactional
    public Result adminQxBoHuiShouLi(Integer qxId, String chuLiYiJian) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 查询申请单
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null){
            return Result.error("申请单不存在");
        }
        // 查看是否为提交状态
        if(!qx.getZhuangTai().equals("提交")) {
            return Result.error("只有提交状态的申请单才可以被驳回");
        }
        // 修改状态
        qx.setZhuangTai("暂存");
        qxMapper.updateQxInfoById(qx);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(qxId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("驳回");
        log.setChuLiYiJian(chuLiYiJian);
        log.setYangPinLeiXing("Q");
        yaopinMapper.addQiXieProcessLog(log);
        return Result.success("驳回成功");
    }

    @Override
    @Transactional
    public Result adminQxTongYiShouLi(Integer qxId, String jianPinBianHao, String chuLiYiJian) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看是否存在
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null){
            return Result.error("申请单不存在");
        }
        // 申请单状态是否为提交
        if(!qx.getZhuangTai().equals("提交")) {
            return Result.error("只有提交状态的申请单才可以同意");
        }
        // 修改状态为受理
        qx.setZhuangTai("受理");
        // 新增检品编号
        qx.setJianPinBianHao(jianPinBianHao);
        // 更新基本信息
        qxMapper.updateQxInfoById(qx);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(qxId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("同意受理");
        log.setChuLiYiJian(chuLiYiJian);
        yaopinMapper.addQiXieProcessLog(log);
        return Result.success("受理成功");
    }

    @Override
    public Result adminQxJianYanWanCheng(Integer qxId, String baoGaoShuFuJian) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看是否存在
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null){
            return Result.error("申请单不存在");
        }
        // 申请单状态是否为受理
        if(!qx.getZhuangTai().equals("受理")) {
            return Result.error("只有受理状态的申请单才可以完成检验");
        }
        // 修改状态
        qx.setZhuangTai("检验完成");
        // 新增检验报告链接
        qx.setBaoGaoShuFuJian(baoGaoShuFuJian);
        // 更新基本信息
        qxMapper.updateQxInfoById(qx);
        // 创建日志
        ProcessLogs log = new ProcessLogs();
        log.setYangPinId(qxId);
        log.setYongHuId(getUserIdByToken());
        log.setCaoZuoLeiXing("检验完成");
        log.setChuLiYiJian(baoGaoShuFuJian);
        qxMapper.addProcessLogs(log);
        return Result.success("检验完成成功");
    }

    @Override
    public Result adminQxGetProcessLogsById(Integer qxId) {
        // 查看是否为管理员
        if(!getUserRoleByToken().equals("admin")) {
            return Result.error("权限不足");
        }
        // 查看是否存在
        QxInfo qx = qxMapper.findQxInfoById(qxId);
        if(qx == null){
            return Result.error("申请单不存在");
        }
        // 获取器械日志
        // 创建pageBean对象
        PageBean<ProcessLogs> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<ProcessLogs> as = qxMapper.getQxProcessLogsById(qxId);
        // 数据列表和总数、页数合并
        Page<ProcessLogs> p = (Page<ProcessLogs>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);

    }

    @Override
    public Result userQxGetProcessLogsById(Integer qxId) {
        // 查看是否为用户权限
        if(!getUserRoleByToken().equals("customer")){
            return Result.error("权限不足");
        }
        // 根据创建人和所给的id查询数据库
        QxInfo qx = qxMapper.selectByIdAndCreator(qxId, getUserIdByToken());
        if(qx == null) {
            return Result.error("申请单不存在");
        }
        // 获取器械日志
        // 创建pageBean对象
        PageBean<ProcessLogs> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<ProcessLogs> as = qxMapper.getQxProcessLogsById(qxId);
        // 数据列表和总数、页数合并
        Page<ProcessLogs> p = (Page<ProcessLogs>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }

    @Override
    public Result adminQxGetJinDuById(Integer qxId) {
        // 查看是否为用户权限
        if(!getUserRoleByToken().equals("admin")){
            return Result.error("权限不足");
        }
        // 查看样品是否存在
        QxInfo qxinfo = qxMapper.findQxInfoById(qxId);
        if(qxinfo == null) {
            return Result.error("申请单不存在");
        }
        // 获取器械日志
        // 创建pageBean对象
        PageBean<ProcessLogs> pb = new PageBean<>();
        // 开启分页查询，自动��接到sql语句中，不需要在sql中手动写入
        PageHelper.startPage(1, 200);
        // 获取查询者的id，只查询id创建的数据
        List<ProcessLogs> as = qxMapper.getQxProcessLogsById(qxId);
        // 数据列表和总数、页数合并
        Page<ProcessLogs> p = (Page<ProcessLogs>) as;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return Result.success(pb);
    }


}
